<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

//路由配置
$route_admin_config = [
    //后台
    'admins' => [
        'controller'    => ['Home','User','Coupon'],
        'page'          => [
            'home/test', //测试
            'home/login', //登录
            'home/captcha', //验证码图片
        ],
        'auth'          => 'admin',
        'middleware'    => [],
        'version'       => ''
    ],
    //前端API
    'apps'   => [
        'controller'    => ['Member'],
        'page'          => [
            'member/wechat/login', //登录
            'member/send/code', //发送验证码
            'member/wechat/bind', //绑定手机号
        ],
        'auth'          => 'app',
        'middleware'    => [],
        'version'       => ''
    ],

];

collect($route_admin_config)->each(function ($item,$index){

    Route::group([
        'prefix'    => $index,
        'namespace' => 'App\Http\Controllers\\'.ucfirst($index)
    ],function () use ($item,$index){

        //生成路由数组
        $route =  collect($item['controller'])->map(function ($controller) use ($item,$index) {

            $class = get_class_methods('\\App\\Http\\Controllers\\'.ucfirst($index).($item['version'] ? '\\'.$item['version'] : '').'\\'.$controller);

            $method = array_slice($class,0,array_search('turn',$class));

            //反射全部路由方法
            return collect($method)->map(function ($i) use ($controller,$item) {

                $active = preg_split("/(?=[A-Z])/", $i);
                $uri =  [
                    'url'       => strtolower($controller.'/'.implode('/',$active)),
                    'active'    => ($item['version'] ? $item['version'].'\\' : '').$controller.'@'.$i,
                ];

                //生成路由
                if (in_array($uri['url'],$item['page'])){
                    //单页
                    Route::any($uri['url'],$uri['active']);
                }else{
                    //auth授权
                    Route::any($uri['url'],$uri['active'])
                        ->middleware(['jwt.role:'.$item['auth']])->middleware($item['middleware']);
                }

                return $uri;
            });
        })->flatten(1);

    });

});
